package com.emmmya.ocs.modules.system.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.emmmya.ocs.common.base.BaseEntity;
import lombok.Data;

import javax.persistence.*;
import java.util.List;

@Data
@Entity
@TableName("h_role")
@Table(name = "h_role")
@JsonIgnoreProperties(value={"users","permissions"})
public class Role extends BaseEntity {

    @Column(name = "role_name", length = 255)
    private String roleName;

    /**
     * 备注
     */
    @Column(name = "remarks",length = 255)
    private String remarks;


    /**
     * 是否作为默认用户的角色
     */
    @Column(name = "default_role")
    private Boolean defaultRole = false;

    /**
     * 角色对应权限集合
     */
    @ManyToMany(
            cascade = CascadeType.REFRESH,
            mappedBy = "roles",//通过维护端的属性关联
            fetch = FetchType.LAZY)
    @JsonBackReference
    @JsonIgnore //json解析过滤此项
    @TableField(exist = false)
    private List<User> users;

    @ManyToMany(mappedBy = "roles", cascade = CascadeType.REFRESH,fetch = FetchType.EAGER)
    @JsonBackReference
    @JsonIgnore //json解析过滤此项
    @TableField(exist = false)
    private List<Permissions> permissions;

    /**
     * Role下所有的menu
     */
    @ManyToMany (cascade = CascadeType.REFRESH)
    @JoinTable(name = "h_role_menu", inverseJoinColumns = @JoinColumn(name = "menu_id"), joinColumns = @JoinColumn(name = "role_id "))
    @JsonIgnore //json解析过滤此项
    @TableField(exist = false)
    private List<Menu> menus;


    @Override
    public String toString() {
        return "Role{" +
                "roleName='" + roleName + '\'' +
                ", remarks='" + remarks + '\'' +
                ", defaultRole=" + defaultRole +
                ", menus=" + menus +
                '}';
    }
}
